package cn.gzsxt.music.mapper;

import java.util.Arrays;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import cn.gzsxt.music.query.UserQueryObject;
@Mapper
public class UserProvider {
	public String selectUserListByQo(UserQueryObject qo) {
		StringBuilder sql = new StringBuilder();
		sql.append("select * from mc_user where 1 = 1");
		if (StringUtils.isNotBlank(qo.getSname())) {
			sql.append(" and user_name like \"%\"#{sname}\"%\"");
		}
		if (StringUtils.isNoneBlank(qo.getSdate())) {
			sql.append(" and user_date = #{sdate}");
		}
		if (StringUtils.isNotBlank(qo.getSgender())) {
			sql.append(" and user_gender = #{sgender}");
		}
		sql.append(" limit #{beginIndex},#{rows}");
		return sql.toString();
	}
	
	public String getUserTotalByQo(UserQueryObject qo) {
		StringBuilder sql = new StringBuilder();
		sql.append("select count(*) from mc_user where 1 = 1");
		if (StringUtils.isNotBlank(qo.getSname())) {
			sql.append(" and user_name like \"%\"#{sname}\"%\"");
		}
		if (StringUtils.isNoneBlank(qo.getSdate())) {
			sql.append(" and user_date = #{sdate}");
		}
		if (StringUtils.isNotBlank(qo.getSgender())) {
			sql.append(" and user_gender = #{sgender}");
		}
		return sql.toString();
	}
	
	public String deleteUserByIds(Map<String, Object> userIdMap) {
		String[] user_ids = (String[]) userIdMap.get("user_ids");
		StringBuffer ids = new StringBuffer();
		for (int i = 0; i < user_ids.length; i++) {
			ids.append(user_ids[i]);
			ids.append(",");
		}
		ids.deleteCharAt(ids.length()-1);
		StringBuffer sql = new StringBuffer();
		sql.append("delete from mc_user where user_id in ("+ids+")");
		return sql.toString();
	}
}
